home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / progjrn / pj_6_3.arc / TIMER2 < prev   
Text File  |  1988-04-05  |  5KB  |  201 lines

  1. Code from Redirections, "Pink Noise" by Frank D. Greco.
  2. Copyright 1988 by Frank D. Greco.  No commercial use of this code
  3. without express permission of the author. 
  4.  
  5.  
  6.   1  /*
  7.   2   * clock.h --  (real) Quick and (really) Dirty macros
  8.   3   */
  9.   4  
  10.   5  #define MSN(x)            ( (x) >> 4 )
  11.   6  #define LSN(x)            ( (x) & 0x0f )
  12.   7  #define LSB(x)            ( (x) & 0x00ff )
  13.   8  #define MSB(x)            ( (x) >> 8 )
  14.   9  
  15.  10  #define BCD(x)            ( (((x) / 10) << 4)  |  ((x) % 10) )
  16.  11  #define UNBCD(x)        (MSN(x)*10 + LSN(x))
  17.  12  
  18.  13  #define CLOCK_RATE        1193180        /* 1,193,180 Hz or 1.19 MHz */
  19.  14  #define STD_PULSE        -1            /* 65536  or 0xFFFF */
  20.  15  #define RESTORE_PULSE()    setpulse(STD_PULSE);
  21.  
  22.  
  23.  
  24.   1  /*
  25.   2   * test.c -- Driver program to show how to use new timing facilities
  26.   3   *
  27.   4   * For the PC, compile as follows:
  28.   5   *
  29.   6   *        C:\> cl -DPC -DFAST_CLOCK test.c gettime.c setpulse.c fixtix.c
  30.   7   */
  31.   8  
  32.   9  #include <stdio.h>
  33.  10  #include <process.h>
  34.  11  #include <dos.h>
  35.  12  #include "timer.h"
  36.  13  #include "clock.h"
  37.  14  
  38.  15  #define NO_ERR        0        /* No error to return to the shell */
  39.  16  #define NO_ARGS        1        /* If nothing to time, return this value */
  40.  17  
  41.  18  main(argc,argv)
  42.  19  char **argv;
  43.  20  {
  44.  21      char *bp;                    /* Ptr to command to be spawned */
  45.  22      unsigned long gettime();    /* Gets clock ticks from MS-DOS */
  46.  23      int i;
  47.  24  
  48.  25      if (argc == 1)        /* If nothing to time, bye-bye */
  49.  26          exit(NO_ARGS);
  50.  27  
  51.  28      bp = argv[1];
  52.  29  
  53.  30  #ifdef PC
  54.  31      setpulse(1000);        /* Set timing granularity to 1/1000 of a second */
  55.  32  #endif
  56.  33  
  57.  34  /* Test #1 */
  58.  35      TIME(spawnvp(P_WAIT, bp, ++argv), bp);
  59.  36  
  60.  37  /* Test #2 */
  61.  38      START();
  62.  39      for (i=0; i<250;i++)
  63.  40      {
  64.  41          printf("\r  %d  ", i);
  65.  42      }
  66.  43      STOP();
  67.  44      PRINT_TIME("for loop");
  68.  45  
  69.  46  /* Test #3 */
  70.  47      TIME(printf("Hello World\n"), "printf call");
  71.  48  
  72.  49  /* Test #4 */
  73.  50      TIME(func1(), "Call to func1() using integer variable");
  74.  51  
  75.  52  /* Test #5 */
  76.  53      TIME(func2(), "Call to func2() using register variable");
  77.  54      
  78.  55  #ifdef PC
  79.  56      setpulse(STD_PULSE);/* Reset timing granularity back to 1/18 of a second */
  80.  57      fixtix();            /* Adjust BIOS view of time */
  81.  58  #endif
  82.  59      exit(NO_ERR);        /* Good Housekeeping */
  83.  60  }
  84.  61  /*****************************************/
  85.  62  func1()
  86.  63  {
  87.  64      int i;
  88.  65  
  89.  66      for (i = 0; i <1000; i++)
  90.  67          ;
  91.  68  }
  92.  69  /*****************************************/
  93.  70  func2()
  94.  71  {
  95.  72      register int i;
  96.  73  
  97.  74      for (i = 0; i <1000; i++)
  98.  75          ;
  99.  76  }
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.   1  /*
  107.   2   * gettime.c  --    Uses BIOS interrupt 0x1A to get the timer count 
  108.   3   *                    in clock ticks.
  109.   4   */
  110.   5  
  111.   6  #include <dos.h>
  112.   7  
  113.   8  #define        TOD_INT        0x1a
  114.   9  
  115.  10  unsigned long gettime()
  116.  11  {
  117.  12      union REGS in, out;
  118.  13      unsigned long current_tix;        /* 32-bit tick value to be returned */
  119.  14  
  120.  15      in.h.ah = 0;                    /* AH = 0    GET clock ticks */
  121.  16      int86(TOD_INT, &in, &out);        /* Call BIOS timer tick interrupt */ 
  122.  17  
  123.  18      current_tix = ( (long) out.x.cx << 16 ) + out.x.dx;
  124.  19  
  125.  20  /* If midnite passes, add a day's worth of clock tix to the return value */
  126.  21  
  127.  22      return ( out.x.ax & 0xff ) ? current_tix += 0x01800b0L: current_tix;
  128.  23  }
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.   1  /*
  136.   2   * fixtix.c  --  Reset the BIOS view of the clock ticks.
  137.   3   *                 Needed after programming the timer chip to pulse faster.
  138.   4   */
  139.   5  
  140.   6  #include <dos.h>
  141.   7  #include <stdio.h>
  142.   8  #include "clock.h"
  143.   9  
  144.  10  #define AH        regs.h.ah
  145.  11  #define CH        regs.h.ch
  146.  12  #define CL        regs.h.cl
  147.  13  #define DH        regs.h.dh
  148.  14  #define DL        regs.h.dl
  149.  15  
  150.  16  union REGS regs;
  151.  17  
  152.  18  fixtix()
  153.  19  {
  154.  20      long far *fp;
  155.  21      long hrs, mins, secs, hsecs, total, tix;
  156.  22  
  157.  23      FP_SEG(fp) = 0x0040;    /* Point to BIOS data area where tix are stored */
  158.  24      FP_OFF(fp) = 0x006C;
  159.  25  
  160.  26      AH = 2;                    /* GET real time clock */
  161.  27  
  162.  28      int86(0x1a, ®s, ®s);
  163.  29  
  164.  30  /* Convert everything to 1/100 seconds */
  165.  31  
  166.  32      hrs = (long) 60 * 60 * 100 * UNBCD(CH);
  167.  33      mins = (long) 60 * 100 * UNBCD(CL);
  168.  34      secs = (long) 100 * UNBCD(DH);
  169.  35      hsecs = (long) UNBCD(DL);
  170.  36  
  171.  37  /* Total them up and convert to clock ticks */
  172.  38  
  173.  39      total = hrs + mins + secs + hsecs;
  174.  40      tix = (long) total * .1820648;
  175.  41   
  176.  42  /* "Poke" the tick value into the BIOS data area */
  177.  43  
  178.  44      *fp = tix;
  179.  45  }
  180.  
  181.  
  182.  
  183.  
  184.  
  185.   1  /*
  186.   2   * setpulse.c  --  Will program the 8254 to Pulse at a desired rate.
  187.   3   */
  188.   4  #include "clock.h"
  189.   5  
  190.   6  setpulse(num)
  191.   7  int num;            /* desired timing resolution */
  192.   8  {
  193.   9      int ax;
  194.  10  
  195.  11      ax = (num == STD_PULSE ? 0xFFFF : (CLOCK_RATE / num));
  196.  12  
  197.  13      outp( 0x43, 0x36);        /* 0011 0110 */
  198.  14      outp( 0x40, LSB(ax) );    /* First the Least Significant Byte */
  199.  15      outp( 0x40, MSB(ax) );    /* Then the MSB */
  200.  16  }
  201.